var app = getApp()
Page({
  data: {
    searchVal: "",
    levelData: [],
    activeData: [],
    active: "",
    id: "",
    show: false
  },
  onLoad(options) {
    this.setData({
      id: options.id
    });
    this.getLevelData();
  },
  // 获取等级
  getLevelData() {
    let url = this.data.id ? "plugin.new-blind-box.frontend.activity.activity-level" : "plugin.new-blind-box.frontend.activity.level";
    let form =  this.data.id ? { id: this.data.id } : ""
    app._postNetWork({
      url: app.getNetAddresss(url),
      data: {...form},
      success: ({data}) => {
        if(data.result) {
          this.setData({
            active: data.data.list[0].id,
            levelData: data.data.list,
            show: true
          })
          this.searchActivityGoods();
        }else {
          wx.showToast({
            icon: 'none',
            title: data.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  searchActivityGoods() {
    let url = this.data.id ? "plugin.new-blind-box.frontend.activity.search-activity-goods" : "plugin.new-blind-box.frontend.activity.search-level-goods";
    let form =  this.data.id ? {id:this.data.id ,level_id : this.data.active, search : {
          goods_keyword : this.data.searchVal
        }} : 
        {level_id : this.data.active, search : {
          goods_keyword : this.data.searchVal
        }}
    app._postNetWork({
      url: app.getNetAddresss(url),
      data: {...form},
      success: ({data}) => {
        if(data.result) {
          this.setData({
            activeData: data.data.goods ? data.data.goods : data.data.list
          })
        }else {
          wx.showToast({
            icon: 'none',
            title: data.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  // 导航栏
  onChangeTab(event) {
    this.setData({
      active: event.detail.name
    })
    this.searchActivityGoods();
  },
  // 搜索
  onChangeSearch(event) {
    this.setData({
      searchVal: event.detail
    })
  }
})